package ch02.test01;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 守护线程
 *
 * @author lwj
 * @date 2025-03-07
 * @vesion 1.0.0
 */
public class GuardThread extends Thread{
    /**
     * 用户线程
     */
    private UserThread userThread;

    // 构造方法
    public GuardThread(UserThread userThread) {
        this.userThread = userThread;
        // 开启守护线程
        this.setDaemon(true);
    }

    @Override
    public void run() {
        try {
            while (userThread.isAlive()) { // 当拷贝线程存活时持续监控
                System.out.printf("拷贝进度: %d%%\n", userThread.getCopySize());
                Thread.sleep(50);
            }
            System.out.println("文件拷贝完成，守护线程退出");
        } catch (InterruptedException e) {
            System.out.println("守护线程被中断");
        }
    }
}
